package com.procurement.material.controller;

import java.util.List;
import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.RenotePcmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.procurement.material.domain.PcmMaterialRecord;
import com.procurement.material.service.IPcmMaterialRecordService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 物料档案Controller
 * 
 * @author liyang
 * @date 2023-12-05
 */
@RestController
@RequestMapping("/record")
public class PcmMaterialRecordController extends BaseController
{
    @Autowired
    private IPcmMaterialRecordService pcmMaterialRecordService;

    @Resource
    private RenotePcmService pcmService;
    /**
     * 查询物料档案列表
     */
    @RequiresPermissions("material:record:list")
    @GetMapping("/list")
    public TableDataInfo list(PcmMaterialRecord pcmMaterialRecord)
    {
        startPage();
        List<PcmMaterialRecord> list = pcmMaterialRecordService.selectPcmMaterialRecordList(pcmMaterialRecord);
        return getDataTable(list);
    }

    /**
     * 导出物料档案列表
     */
    @RequiresPermissions("material:record:export")
    @Log(title = "物料档案", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PcmMaterialRecord pcmMaterialRecord)
    {
        List<PcmMaterialRecord> list = pcmMaterialRecordService.selectPcmMaterialRecordList(pcmMaterialRecord);
        ExcelUtil<PcmMaterialRecord> util = new ExcelUtil<PcmMaterialRecord>(PcmMaterialRecord.class);
        util.exportExcel(response, list, "物料档案数据");
    }

    /**
     * 获取物料档案详细信息
     */
    @RequiresPermissions("material:record:query")
    @GetMapping(value = "/{productNumber}")
    public AjaxResult getInfo(@PathVariable("productNumber") String productNumber)
    {
        return success(pcmMaterialRecordService.selectPcmMaterialRecordByProductNumber(productNumber));
    }

    /**
     * 新增物料档案
     */
    @RequiresPermissions("material:record:add")
    @Log(title = "物料档案", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PcmMaterialRecord pcmMaterialRecord)
    {
        String maxNumber = pcmMaterialRecordService.getMaxProductNumber();
//        int next = Integer.parseInt(maxNumber.substring(maxNumber.length()-5)) + 1;
//        String num = StringUtils.leftPad(String.valueOf(next), 5, "0");
//        String currentYearMonthDay = DateUtils.dateTimeNow("yyyy-MM-dd");
//        String str = currentYearMonthDay.replace("-","");
//        str = "p"+str+num;
        pcmMaterialRecord.setProductNumber(pcmService.scbm("pm", SecurityConstants.INNER));
        pcmMaterialRecord.setSubmitter(SecurityUtils.getUserId());
        return toAjax(pcmMaterialRecordService.insertPcmMaterialRecord(pcmMaterialRecord));
    }

    /**
     * 修改物料档案
     */
    @RequiresPermissions("material:record:edit")
    @Log(title = "物料档案", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PcmMaterialRecord pcmMaterialRecord)
    {
        return toAjax(pcmMaterialRecordService.updatePcmMaterialRecord(pcmMaterialRecord));
    }

    /**
     * 删除物料档案
     */
    @RequiresPermissions("material:record:remove")
    @Log(title = "物料档案", businessType = BusinessType.DELETE)
	@DeleteMapping("/{productNumbers}")
    public AjaxResult remove(@PathVariable String[] productNumbers)
    {
        return toAjax(pcmMaterialRecordService.deletePcmMaterialRecordByProductNumbers(productNumbers));
    }
}
